package by.cooker.chat.logic;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * @author cooker
 */
public class Configuration
{
	/** Configuration instance */
	private static Configuration conf = null;

	/** Properties file name */
	private static final String FILE_NAME = "fruit.conf";

	/** @see java.util.loggimg.Logger */
	private static Logger logger = Logger.getLogger("Configuration");

	public static synchronized Configuration getConf()
	{
		if (conf == null)
		{
			conf = new Configuration();
		}
		return conf;
	}

	private Properties props = null;

	private Configuration()
	{
		props = new Properties();
		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream(new File(FILE_NAME));
			props.load(fis);
			fis.close();
		} catch (IOException e)
		{
			logger.severe("jSquirrel properties file not found!"
					+ "The program can not be started!");
			System.exit(0);
		}
	}

	public synchronized int getInteger(String key)
	{
		if (key == null)
		{
			throw new IllegalArgumentException("Key" + key + " == null");
		}
		int value = 0;
		if (props.containsKey(key))
		{
			Integer integer = new Integer((String) props.get(key));
			value = integer.intValue();
		} else
		{
			logger.severe("Key " + key + " not found!"
					+ " The program may be not work correctly");
		}
		return value;
	}

	/**
	 * @param key
	 *            property name
	 * @param defaultValue
	 *            value by default
	 * @return integer value
	 */
	public synchronized int getInteger(String key, int defaultValue)
	{
		if (key == null || defaultValue < 0)
		{
			throw new IllegalArgumentException("Key" + key + " == null or "
					+ defaultValue + " < 0");
		}
		int value = 0;
		if (props.containsKey(key))
		{
			Integer integer = new Integer((String) props.get(key));
			value = integer.intValue();
		} else
		{
			value = defaultValue;
			logger.severe("Key " + key + " not found! Used value by default!");
		}
		return value;
	}

	public synchronized String getString(String key)
	{
		if (key == null)
		{
			throw new IllegalArgumentException("Key == " + key);
		}
		String value = null;
		if (props.containsKey(key))
		{
			value = (String) props.get(key);
		} else
		{
			logger.severe("Key " + key + " not found!"
					+ "The program may be not work correctly");
		}
		return value;
	}

	public synchronized String getString(String key, String defaultValue)
	{
		if (key == null || defaultValue == null)
		{
			throw new IllegalArgumentException("Key" + key + " == null or "
					+ defaultValue + " == null");
		}
		String value = null;
		if (props.containsKey(key))
		{
			value = (String) props.get(key);
		} else
		{
			value = defaultValue;
			logger
					.severe("Key " + key
							+ "not  found! New value don't written!");
		}
		return value;
	}

	public synchronized User getUser()
	{
		String login = getString("login");
		String password = getString("password");
		String host = getString("host");
		int port = getInteger("port", 6112);
		String channel = getString("channel");
		char[] pass = new char[password.length()];
		for (int i = 0; i < password.length(); i++)
		{
			pass[i] = password.charAt(i);

		}

		return new User(login, pass, host, port, channel);
	}
}
